package com.projects.xenia;

import java.io.FileWriter;
import java.lang.Thread.UncaughtExceptionHandler;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

import com.projects.xenia.global_modules.MainGlobalModule;

import android.app.Application;
import android.content.ContentValues;


public class MyApplication extends Application{

	private UncaughtExceptionHandler defaultUEH;
	
	private void SaveToDBLOG (String ErrorString) {
		
		ContentValues InsertValues = new ContentValues();
		
		InsertValues.put("id",UUID.randomUUID().toString().toUpperCase());
		InsertValues.put("date_save",MainGlobalModule.DateToSqlString(Calendar.getInstance().getTime()));
		InsertValues.put("state",1);
		InsertValues.put("error_content",ErrorString);
		InsertValues.put("addvalue_0","");
		InsertValues.put("addvalue_1","");
		InsertValues.put("addvalue_2","");		
		
		if (MainGlobalModule.DBE!=null){
			MainGlobalModule.DBE.insert("xsys_errors", null, InsertValues);	
		}
		
	}
	
	private Thread.UncaughtExceptionHandler _unCaughtExceptionHandler =
	        new Thread.UncaughtExceptionHandler() {
				@Override
				public void uncaughtException(Thread thread, Throwable ex) {
					
					final DateFormat formatter = new SimpleDateFormat("dd.MM.yy HH:mm");
			    	final Date dumpDate = new Date(System.currentTimeMillis());
			    	
			    	StringBuilder reportBuilder = new StringBuilder();
			    	
			    	reportBuilder
			    		.append(formatter.format(dumpDate)).append("\n")
			    		.append(thread.toString()).append("\n");
			    	
			    	processThrowable(ex, reportBuilder);

			    	//try {
					//	FileWriter writer = new FileWriter(MainGlobalModule.StoragePath.concat("/errorslog.txt"), false);						
					//	writer.write(reportBuilder.toString().concat("\n"));
					//	writer.flush ();
					//	writer.close();				
			    	//} catch (Exception e) {
			    	//	e.printStackTrace();
			    	//}
			    	
			    	SaveToDBLOG (reportBuilder.toString());
			    	
			    	defaultUEH.uncaughtException(thread, ex);
			    	
			    	
				}
		
	};
	
    private void processThrowable(Throwable exception, StringBuilder builder) {
    	
    	if(exception == null) {
    		return;
    	}
    	else {    		
            StackTraceElement[] stackTraceElements = exception.getStackTrace();
    	    builder    	    	
    	    	.append("Exception: ").append(exception.getClass().getName()).append("\n")
    	        .append("Message: ").append(exception.getMessage()).append("\nStacktrace:\n");
    	    for(StackTraceElement element : stackTraceElements) {
    	    	builder.append("\t").append(element.toString()).append("\n");
    	    }
    	    
    	    processThrowable(exception.getCause(), builder);
    	}    
    }
    
    @Override
    public void onCreate() {
    	
    	defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
    	
        Thread.setDefaultUncaughtExceptionHandler(_unCaughtExceptionHandler);
        
        super.onCreate();
        
    }
    
}

